package servlet;

import entity.Finance;
import service.FinanceService;
import service.UserService;
import service.impl.FinanceServiceImpl;
import service.impl.UserServiceImpl;
import util.ScannerUtils;

import java.util.ArrayList;

public class UserFinance {
    UserService userService = new UserServiceImpl();
    FinanceService financeService = new FinanceServiceImpl();
    public void userList(String idNumber) throws Exception{
        System.out.println("个人收支情况：");
        System.out.println("请选择：");
        System.out.println("1.查看历史");
        System.out.println("2.存钱");
        System.out.println("3.取钱");
        String choice = ScannerUtils.scannerUtil();
        if(choice.equals("1")){
            moneyHistory(idNumber,true);
        }else if(choice.equals("2")){
            addMoney(idNumber,null);
        }else if(choice.equals("3")){
            reduceMoney(idNumber,null);
        }
    }
    public void moneyHistory(String idNumber,boolean bool) throws Exception{
        ArrayList<Finance> list = financeService.findHistory(idNumber);
        String selectMoney = userService.selectMoney(idNumber);
        for(int i = 0; i < list.size(); i++){
            System.out.println(list.get(i).getHistory());
        }
        System.out.println("------");
        System.out.println("余额" + selectMoney);
        System.out.println("");
        if(bool==true){
            UserFinance userFinance = new UserFinance();
            userFinance.userList(idNumber);
        }
    }

    public void addMoney(String idNumber, String money) throws Exception{
        if(money == null){
            System.out.println("请输入存入钱数");
            money = ScannerUtils.scannerUtil();
            String history ="存入"+money;
            updateAddMoney(idNumber,money,history);
            UserFinance userFinance = new UserFinance();
            userFinance.userList(idNumber);
        }else{
            String history ="存入"+money;
            updateAddMoney(idNumber,money,history);
        }
    }
    public void reduceMoney(String idNumber, String money) throws Exception{
        if(money == null){
            System.out.println("请输入取出钱数");
            money = ScannerUtils.scannerUtil();
            String history ="取出"+money;
            updateReduceMoney(idNumber,money,history);
            UserFinance userFinance = new UserFinance();
            userFinance.userList(idNumber);
        }else{
            String history ="取出"+money;
            updateReduceMoney(idNumber,money,history);
        }
    }

    public void updateAddMoney(String idNumber, String money, String history) throws Exception{
        String selectMoney = userService.selectMoney(idNumber);
        int selectMoneyInt = Integer.parseInt(selectMoney);
        int moneyInt = Integer.parseInt(money);
        int finalMoneyInt = moneyInt + selectMoneyInt;
        String finalMoney = String.valueOf(finalMoneyInt);
        financeService.insert(idNumber,history);
        userService.updateMoney(idNumber,finalMoney);
        System.out.println("余额" + finalMoney);
        System.out.println("");
    }

    public void updateReduceMoney(String idNumber, String money, String history) throws Exception{
        String selectMoney = userService.selectMoney(idNumber);
        int selectMoneyInt = Integer.parseInt(selectMoney);
        int moneyInt = Integer.parseInt(money);
        int finalMoneyInt = selectMoneyInt - moneyInt;
        String finalMoney = String.valueOf(finalMoneyInt);
        financeService.insert(idNumber,history);
        userService.updateMoney(idNumber,finalMoney);
        System.out.println("余额" + finalMoney);
        System.out.println("");
    }
}
